/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.domain;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.lang.String;

/**
 *
 * @author VuTran
 */
public class Boolean extends Domain {

    @Override
    public boolean validate(Object o) {
        if (o instanceof java.lang.Boolean) {
            return true;
        }
        return false;
    }

    @Override
    public Object convertString(String s) throws DomainConversionException {
        String v = s.toLowerCase();
        if (v.equals("true")) return true;
        if (v.equals("false")) return false;
        return null;
    }

    @Override
    public Class getNaiveType() {
        return boolean.class;
    }

    @Override
    public String getDBType() {
        return "bit";
    }
    
    @Override
    public String toString() {
        return "Boolean";
    }
    
    @Override
    public boolean equals(Object obj) {
        return (obj instanceof edu.pdx.cs.guavadata.domain.Boolean);
    }
    
    @Override
    public int hashCode() {
        int hash = edu.pdx.cs.guavadata.domain.Boolean.class.hashCode();
        return hash;
    }
 
    @Override
    public Domain upcast() {
        return new TinyInteger();
    }

    @Override
    public Object upcastValue(Object o) throws Exception {
        if (o instanceof java.lang.Boolean) {
            if (((java.lang.Boolean) o) == false) {
                return 0;
            }
            return 1;
        }
        
        if (o instanceof java.lang.String) {
            String s = String.valueOf(o).toLowerCase();
            if (s.equals("true")) return 1;
            if (s.equals("false")) return 0;
        }
        
        
        throw new Exception("Object " + o.toString() + "is not a valid boolean object.");
    }
    
    @Override
    public Domain merge(Domain d) {
        if (d instanceof edu.pdx.cs.guavadata.domain.Boolean) {
            return this;
        } 
        return null;
    }

    @Override
    public boolean isOrdered() {
        return false;
    }

    @Override
    public boolean canContain() {
        return false;
    }

    @Override
    public Domain clone() {
        return new Boolean();
    }

    public static Domain fromElement(Element xe) {
        return new Boolean();
    }
    
    @Override
    public Element ToXml(Document xdoc) {
        Element xe = xdoc.createElement("Domain");
        xe.setAttribute("Type", "Boolean");
        return xe;
    }

    @Override
    public List<Object> listOutput() {
        List<Object> lo = new ArrayList<Object>();
        lo.add(true);
        lo.add(false);
        return lo;
    }

    public static Boolean fromDBInfoRow(ResultSet dr) {
        return new Boolean();
    }
}
